//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <numeric>
#include <buola/utility/cunittest.h>
#include <buola/iterator/transform.h>
#include <buola/iterator/filter.h>
#include <boost/range/adaptors.hpp>
#include <buola/functors/predicates/unary.h>
#include <buola/functors/transform/math.h>

class AFilter
{
public:
    typedef bool result_type;
    
    template<typename tType>
    bool operator()(tType pV) const
    {
        return pV>0.0;
    }
};

class ASin
{
public:
    typedef double result_type;

    template<typename tType>
    tType operator()(tType pV) const __attribute__((const));
};

template<typename tType>
tType ASin::operator()(tType pV) const
{
    return std::sin(pV);
}

using namespace buola;

int main(int pArgC,char **pArgs)
{
    buola_init(pArgC,pArgs);
    
    CUnitTest lTest;

    std::vector<double> lV;

    for(int i=0;i<10000000;i++)
    {
        lV.push_back(i);
    }
    
    msg_info() << "normal for loop\n";
    double lA=0.0;
    start_timer();
    for(int i=0;i<lV.size();i++)
    {
        if(std::sin(lV[i])<=0.0) continue;
        lA+=std::sin(lV[i]);
    }
    end_timer();
    msg_info() << lA << "\n";

    msg_info() << "range for loop\n";
    lA=0.0;
    start_timer();
    for(double v : lV)
    {
        if(std::sin(v)<=0.0) continue;
        lA+=std::sin(v);
    }
    end_timer();
    msg_info() << lA << "\n";

    msg_info() << "range for loop with buola range\n";
    lA=0.0;
    start_timer();
    for(double v : range(lV))
    {
        if(std::sin(v)<=0.0) continue;
        lA+=std::sin(v);
    }
    end_timer();
    msg_info() << lA << "\n";

    msg_info() << "range for loop with buola transform range\n";
    lA=0.0;
    start_timer();
    for(double v : transform_range(lV,ASin()))
    {
        if(v<=0.0) continue;
        lA+=v;
    }
    end_timer();
    msg_info() << lA << "\n";

    msg_info() << "range for loop with buola filter and transform range\n";
    lA=0.0;
    start_timer();
    for(double v : filter_range(transform_range(lV,ASin()),AFilter()))
    {
        lA+=v;
    }
    end_timer();
    msg_info() << lA << "\n";

    msg_info() << "range for loop with buola filter and transform range (automatic)\n";
    lA=0.0;
    start_timer();
    for(double v : lV|fn::ASin()|fn::greater_than(0.0))
    {
        lA+=v;
    }
    end_timer();
    msg_info() << lA << "\n";

    msg_info() << "accumulate with buola filter range";
    auto lRange=filter_range(transform_range(lV,ASin()),AFilter());
    start_timer();
    lA=std::accumulate(lRange.begin(),lRange.end(),0.0);
    end_timer();
    msg_info() << lA << "\n";

    msg_info() << "accumulate with buola filter range (pipe syntax)";
    auto lRange2=lV|transform(ASin())|filter(AFilter());
    start_timer();
    lA=std::accumulate(lRange2.begin(),lRange2.end(),0.0);
    end_timer();
    msg_info() << lA << "\n";
    
    msg_info() << "range for loop with boost filter and transform range";
    lA=0.0;
    start_timer();
    for(double v : lV | boost::adaptors::transformed(ASin()) | boost::adaptors::filtered(AFilter()))
    {
        lA+=v;
    }
    end_timer();
    msg_info() << lA << "\n";

    for(char i : "ABC" | filter(!fn::equal_to('B')))
        msg_info() << i;
    msg_info() << "\n";

    return lTest.ReturnCode();
}
